/*     */ package com.java.diagnostics.visualizer.parser.xml;
/*     */ 
/*     */ import com.java.diagnostics.visualizer.data.DataLevel;
/*     */ import com.java.diagnostics.visualizer.data.SourceData;
/*     */ import com.java.diagnostics.visualizer.data.StructuredData;
/*     */ import com.java.diagnostics.visualizer.data.StructuredDataBuilder;
/*     */ import com.java.diagnostics.visualizer.data.TupleData;
/*     */ import com.java.diagnostics.visualizer.data.UnstructuredData;
/*     */ import com.java.diagnostics.visualizer.factory.DataFactory;
/*     */ import com.java.diagnostics.visualizer.factory.SourceDataFactory;
/*     */ import com.java.diagnostics.visualizer.gc.defaultextensions.util.Messages;
/*     */ import com.java.diagnostics.visualizer.impl.factory.LogFactory;
/*     */ import com.java.diagnostics.visualizer.parser.vgc.InstanceVGCParser;
/*     */ import com.java.diagnostics.visualizer.parser.vgc.VGCDataManager;
/*     */ import com.java.diagnostics.visualizer.parser.vgc.VGCParserPreferenceHelper;
/*     */ import com.java.diagnostics.visualizer.parser.vgc.constants.VGCGCReasons;
/*     */ import com.java.diagnostics.visualizer.parser.vgc.j9.J950DateConverter;
/*     */ import com.java.diagnostics.visualizer.properties.OutputProperties;
/*     */ import com.java.diagnostics.visualizer.sources.Source;
/*     */ import java.text.MessageFormat;
/*     */ import java.util.Date;
/*     */ import java.util.logging.Level;
/*     */ import java.util.logging.Logger;
/*     */ import org.xml.sax.Attributes;
/*     */ 
/*     */ public abstract class VGCXMLParser extends GCAndMemoryVisualizerXMLParser
/*     */   implements InstanceVGCParser
/*     */ {
/*  41 */   public static final SourceDataFactory SOURCE_DATA_FACTORY = SourceDataFactory.getFactory();
/*     */   private static final String GLOBAL = "global";
/*     */   protected static final String IBM_J9 = "IBM J9 ";
/*  45 */   public static final Logger TRACE = LogFactory.getTrace(VGCXMLParser.class);
/*     */ 
/*  47 */   protected final String className = getClass().getName();
/*     */   protected static final int UNSET = -1;
/*     */   protected static final String INTERVALMS = "intervalms";
/*     */   protected static final String TYPE = "type";
/*     */   protected static final String GC = "gc";
/*     */   protected static final String VERBOSEGC_OPEN = "<verbosegc";
/*     */   protected static final String VERBOSEGC_CLOSE = "</verbosegc>";
/*     */   protected static final String VERBOSEGC = "verbosegc";
/*  58 */   protected static final MessageFormat SCOPE_COMMENT = new MessageFormat(
/*  59 */     Messages.getString("StrictJ950VGCParser.scope.comment"));
/*     */ 
/*  61 */   protected static final MessageFormat REASON_COMMENT = new MessageFormat(
/*  62 */     Messages.getString("StrictJ950VGCParser.reason.comment"));
/*     */ 
/*  64 */   protected static final MessageFormat COMBINED_COMMENT = new MessageFormat(
/*  65 */     Messages.getString("StrictJ950VGCParser.combined.comment"));
/*     */ 
/*  68 */   protected static final String VM_RESTART_WARNING_CONTENTS = Messages.getString("StrictJ950VGCParser.vm.restart.warning");
/*     */   protected static final String EMPTY_STRING = "";
/*     */   public static final int MAX_SOURCE_SIZE = 3145728;
/*     */   public static final String CLOSE_ANGLE_BRACKET = ">";
/*     */   public static final String OPEN_ANGLE_BRACKET = "<";
/*     */   public static final String TEMP_FILE_PREFIX = "j9vgcparser";
/*     */   public static final String FILENAME_EXTENSION = ".vgc";
/*  79 */   private static final String VERSION = Messages.getString("VGCXMLParser.j9.version");
/*     */   protected static final String ID = "id";
/*     */   protected static final String TIMESTAMP = "timestamp";
/*     */   protected static final String METRONOME_VERSION_SUFFIX = "-Metronome";
/*     */   protected static final String XML_CONTENT_TYPE = "xml";
/*  86 */   private static final VGCParserPreferenceHelper PLUGIN_PREFERENCES = new VGCParserPreferenceHelper();
/*     */   protected static final String SWEEP = "sweep";
/*     */   protected static final String MARK = "mark";
/*     */   protected static final String COMPACT = "compact";
/*     */   protected static final String WEAK = "weak";
/*     */   protected static final String PHANTOM = "phantom";
/*     */   protected static final String SOFT = "soft";
/*     */   protected static final String MOVEBYTES = "movebytes";
/*     */   protected static final String MOVECOUNT = "movecount";
/*     */   protected static final String NURSERY = "nursery";
/*     */   protected static final String BYTES = "bytes";
/*     */   protected static final String SOA = "soa";
/*     */   protected static final String LOA = "loa";
/*     */   protected static final String TOTAL = "total";
/*     */   protected static final String CLASSLOADERSUNLOADED = "classloadersunloaded";
/*     */   protected static final String CLASSESUNLOADED = "classesunloaded";
/*     */   protected static final String TENURED = "tenured";
/*     */   protected static final String FINALIZATION = "finalization";
/*     */   protected static final String TENUREAGE = "tenureage";
/*     */   protected static final String TILTRATIO = "tiltratio";
/*     */   protected static final String NAME = "name";
/*     */   protected static final String VALUE = "value";
/*     */   protected static final String ATTRIBUTE = "attribute";
/*     */   protected static final String SYSTEM = "system";
/*     */   protected static final String WARNING = "warning";
/*     */   protected static final String DETAILS = "details";
/*     */   protected String currentComment;
/*     */   protected int currentGCNumber;
/*     */   protected boolean isGoodData;
/*     */   protected double[] timestamps;
/*     */   protected double[] durations;
/* 125 */   protected int restartCount = 0;
/* 126 */   protected int reasonIndex = -1;
/*     */   protected boolean interpolateTimestamps;
/*     */   protected boolean filterSystemGCs;
/*     */   protected String version;
/*     */   protected OutputProperties properties;
/*     */   protected VGCDataManager dataManager;
/*     */   protected StructuredDataBuilder gcAttributes;
/*     */   protected StructuredDataBuilder systemAttributes;
/* 134 */   protected boolean inSystemAttributes = false;
/*     */ 
/*     */   protected double convertStringToNumber(String stringValue)
/*     */   {
/* 142 */     double value = -1.0D;
/*     */ 
/* 144 */     if ((stringValue != null) && (stringValue.length() > 0)) {
/*     */       try {
/* 146 */         value = Double.parseDouble(stringValue);
/*     */       }
/*     */       catch (NumberFormatException localNumberFormatException)
/*     */       {
/* 153 */         value = J950DateConverter.parseDate(stringValue);
/*     */       }
/*     */     }
/* 156 */     return value;
/*     */   }
/*     */ 
/*     */   protected void initialiseGeneralSourceData(Source source, OutputProperties properties)
/*     */   {
/* 161 */     this.currentComment = "";
/*     */ 
/* 163 */     this.currentGCNumber = 0;
/*     */ 
/* 165 */     this.timestamps = new double[VGCGCReasons.maxInt() + 1];
/* 166 */     this.durations = new double[VGCGCReasons.maxInt() + 1];
/* 167 */     this.restartCount = 0;
/* 168 */     initialiseSpecificSourceData(properties);
/*     */   }
/*     */ 
/*     */   protected abstract void initialiseSpecificSourceData(OutputProperties paramOutputProperties);
/*     */ 
/*     */   protected final void addData(TupleData data)
/*     */   {
/* 175 */     this.sourceData.addData(data, DataLevel.VARIANT);
              //added by wenguang.huang 
              this.sourceData.addData(data, DataLevel.DATASET);
			//added end by wenguang.huang 


/*     */   }
/*     */ 
/*     */   protected final void addData(StructuredData data) {
/* 179 */     this.sourceData.addData(data, DataLevel.VARIANT);
             //added by wenguang.huang 
              this.sourceData.addData(data, DataLevel.DATASET);
             //added end by wenguang.huang 
/*     */   }
/*     */ 
/*     */   protected final void addData(UnstructuredData data) {
/* 183 */     this.sourceData.addData(data, DataLevel.VARIANT);
              //added by wenguang.huang 
              this.sourceData.addData(data, DataLevel.DATASET);
              //added end by wenguang.huang 
/*     */   }
/*     */ 
/*     */   protected String stripBadCharacters(String string)
/*     */   {
/* 193 */     return string;
/*     */   }
/*     */ 
/*     */   protected void calculateTimestamp(Attributes atts, String reason, double intervalInMilliseconds)
/*     */   {
/* 198 */     double currentTimeStamp = -1.0D;
/* 199 */     this.reasonIndex = VGCGCReasons.nameToInt(reason);
/*     */ 
/* 204 */     String type = atts.getValue("type");
/*     */ 
/* 206 */     if ("global".equals(type)) {
/* 207 */       this.reasonIndex = this.timestamps.length;
/*     */     }
/*     */ 
/* 210 */     double nominalTimeStamp = convertStringToNumber(
/* 211 */       atts.getValue("timestamp"));
/*     */ 
/* 213 */     if ((this.reasonIndex >= 0) && (this.interpolateTimestamps)) {
/* 214 */       currentTimeStamp = interpolateTimestamp(intervalInMilliseconds, 
/* 215 */         nominalTimeStamp);
/*     */     } else {
/* 217 */       if (this.reasonIndex == -1) {
/* 218 */         TRACE.warning("Could not calculate timestamp for " + reason);
/*     */       }
/* 220 */       currentTimeStamp = nominalTimeStamp;
/*     */     }
/*     */ 
/* 223 */     if (currentTimeStamp != -1.0D)
/* 224 */       this.dataManager.setX(currentTimeStamp);
/*     */   }
/*     */ 
/*     */   private double interpolateTimestamp(double intervalInMilliseconds, double nominalTimeStamp)
/*     */   {
/* 230 */     double previousTimestamp = this.dataManager.getCurrentX();
/*     */ 
/* 232 */     double interval = intervalInMilliseconds;
/* 233 */     double relevantTimestamp = this.timestamps[this.reasonIndex];
/*     */     double currentTimeStamp;
/* 234 */     if (relevantTimestamp > 0.0D)
/*     */     {
/* 240 */       double pause = this.durations[this.reasonIndex];
/* 241 */       double derivedTimeStamp = relevantTimestamp + (interval + pause);
/*     */       //double currentTimeStamp;
/*     */       //double currentTimeStamp;
/* 248 */       if ((Math.abs(nominalTimeStamp - derivedTimeStamp) < 1000.0D) && 
/* 249 */         (derivedTimeStamp >= previousTimestamp)) {
/* 250 */         currentTimeStamp = derivedTimeStamp;
/*     */       }
/*     */       else
/*     */       {
/*     */         //double currentTimeStamp;
/* 253 */         if (derivedTimeStamp < previousTimestamp) {
/* 254 */           if (TRACE.isLoggable(Level.FINER)) {
/* 255 */             TRACE.finer("Time appears to go backwards for type " + 
/* 256 */               VGCGCReasons.intToName(this.reasonIndex) + 
/* 257 */               " by " + (
/* 258 */               previousTimestamp - derivedTimeStamp) + 
/* 259 */               " s. \nReported interval is " + interval + 
/* 260 */               " ms.\nReported duration of last event is " + 
/* 261 */               pause + 
/* 262 */               " ms.\n" + "Derived: ");
                        //+ 
/* 263 */               //new Date(()(1000.0D * derivedTimeStamp)) + 
/* 264 */               //"\n previous: " + 
/* 265 */               //new Date(()(1000.0D * previousTimestamp)) + 
/* 266 */               //"\n last current: " + 
/* 267 */               //new Date(()(1000.0D * relevantTimestamp)));
/*     */           }
/* 269 */           currentTimeStamp = previousTimestamp;
/*     */         }
/*     */         else
/*     */         {
/* 273 */           if (TRACE.isLoggable(Level.FINER)) {
/* 274 */             TRACE.finer("Slipped interpolating timestamp on type " + 
/* 275 */               VGCGCReasons.intToName(this.reasonIndex) + 
/* 276 */               " by " + (
/* 277 */               nominalTimeStamp - derivedTimeStamp) + 
/* 278 */               " s. \nReported interval is " + interval + 
/* 279 */               " ms.\nReported duration of last event is " + 
/* 280 */               pause + 
/* 281 */               " ms.\n" + "Derived: ");
                        //+ 
/* 282 */               //new Date(()(1000.0D * derivedTimeStamp)) + 
/* 283 */               //"\n nominal: " + 
/* 284 */               //new Date(()(1000.0D * nominalTimeStamp)) + 
/* 285 */               //"\n last current: " + 
/* 286 */               //new Date(()(1000.0D * relevantTimestamp)));
/*     */           }
/*     */ 
/* 289 */           currentTimeStamp = 
/* 290 */             Math.max(nominalTimeStamp, previousTimestamp);
/*     */         }
/*     */ 
/*     */       }
/*     */ 
/* 295 */       this.timestamps[this.reasonIndex] = currentTimeStamp;
/*     */     }
/*     */     else
/*     */     {
/* 299 */       currentTimeStamp = Math.max(nominalTimeStamp, previousTimestamp);
/* 300 */       this.timestamps[this.reasonIndex] = currentTimeStamp;
/*     */     }
/*     */ 
/* 303 */     return currentTimeStamp;
/*     */   }
/*     */ 
/*     */   protected final void addDataPoint(String fieldName, double y, String comment) {
/* 307 */     this.dataManager.addPoint(fieldName, y, comment);
/*     */   }
/*     */ 
/*     */   protected void workOutVersion(Attributes atts) {
/* 311 */     this.version = atts.getValue("version");
/*     */ 
/* 313 */     if (this.version == null)
/*     */     {
/* 315 */       this.version = VERSION;
/*     */     }
/*     */     else
/* 318 */       this.version = ("IBM J9 " + this.version);
/*     */   }
/*     */ 
/*     */   protected void recordVersion()
/*     */   {
/* 324 */     UnstructuredData versionData = factory.createUnstructuredData(
/* 325 */       "VGCLabels.version", Messages.getString("VGCLabels.version"), 
/* 326 */       this.version);
/* 327 */     addData(versionData);
/*     */   }
/*     */ 
/*     */   protected UnstructuredData createAttributeData() {
/* 331 */     return DataFactory.getFactory("VGC")
/* 332 */       .createUnstructuredData("Attributes", 
/* 333 */       Messages.getString("attributes"));
/*     */   }
/*     */ 
/*     */   protected void initialisePreferences() {
/* 337 */     this.filterSystemGCs = PLUGIN_PREFERENCES.getFilterSystemGCs();
/* 338 */     this.interpolateTimestamps = PLUGIN_PREFERENCES.getInterpolateTimes();
/*     */   }
/*     */ 
/*     */   protected void addAttribute(String fieldName, String attributeName, Attributes atts)
/*     */   {
/* 343 */     addAttribute(fieldName, attributeName, atts, this.currentComment);
/*     */   }
/*     */ 
/*     */   protected void addAttribute(String fieldName, String attributeName, Attributes atts, String comment)
/*     */   {
/* 348 */     String value = atts.getValue(attributeName);
/* 349 */     if (value != null)
/*     */     {
/* 351 */       double y = convertStringToNumber(value);
/*     */ 
/* 353 */       this.dataManager.addPoint(fieldName, y, comment);
/*     */     }
/*     */   }
/*     */ 
/*     */   protected void handleGCAttribute(String name, Attributes atts) {
/* 358 */     String attName = atts.getValue("name");
/* 359 */     String attValue = atts.getValue("value");
/* 360 */     if ((attName != null) && (attValue != null))
/* 361 */       this.gcAttributes.put(attName, attValue);
/*     */   }
/*     */ 
/*     */   protected void handleSystemAttribute(String name, Attributes atts)
/*     */   {
/* 366 */     String attName = atts.getValue("name");
/* 367 */     String attValue = atts.getValue("value");
/* 368 */     if ((attName != null) && (attValue != null))
/* 369 */       this.systemAttributes.put(attName, attValue);
/*     */   }
/*     */ 
/*     */   protected void initialiseAttributeData()
/*     */   {
/* 374 */     this.gcAttributes = factory
/* 375 */       .createStructuredData("VGCLabels.gc.attributes", 
/* 375 */       Messages.getString("VGCLabels.gc.attributes"));
/* 376 */     this.systemAttributes = factory
/* 378 */       .createStructuredData("VGCLabels.system.attributes", 
/* 378 */       Messages.getString("VGCLabels.system.attributes"));
/*     */   }
/*     */ }

